package jdk21.record;

/**
 * @description: 记录类型模式匹配
 * @author: grf
 * @date: 2024/8/22
 */
public class Record {
    public static void main(String[] args) {
        System.out.println(describePoint(new Point(0, 0)));
        System.out.println(describeLine(new Line(new Point(1, 2), new Point(1, 2))));
    }
    /** 
     * @description: 
     * @author: grf
     * @date: 2024/8/22
     */
    public static String describePoint(Point p) {
        return switch (p) {
            case Point(int x, int y) when x == y -> "Point on the diagonal";
            case Point(int x, int y) -> "Point at (" + x + ", " + y + ")";
            default -> "Unknown point";
        };
    }
    /**
     * @description: switch表达式中使用record
     * @author: grf
     * @date: 2024/8/22
     */
    public static String describeLine(Line line) {
        return switch (line) {
            case Line(Point(int x1, int y1), Point(int x2, int y2))
                    when x1 == x2 && y1 == y2 -> "Degenerate line (point)";
            case Line(Point(int x1, int y1), Point(int x2, int y2)) ->
                    "Line from (" + x1 + ", " + y1 + ") to (" + x2 + ", " + y2 + ")";
            default -> "Unknown line";
        };
    }
}
